package com.mind.customer.template;

// TemplateParser.java

import com.mind.common.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TemplateParser {
    protected static final Pattern VARIABLE_PATTERN = Pattern.compile("\\$\\{([^}]+)\\}");
    protected static final Pattern TABLE_PATTERN = Pattern.compile("\\$\\{tab:([^}]+)\\}");
    protected static final Pattern COLUMN_PATTERN = Pattern.compile("([^|,]+)(\\|([^|,]+))?");

    public static TemplateVariables parseTemplate(String template) {
        TemplateVariables variables = new TemplateVariables();

        // 查找普通变量
        Matcher varMatcher = VARIABLE_PATTERN.matcher(template);
        while (varMatcher.find()) {
            String expression = varMatcher.group(1);
            if (!expression.startsWith("tab:")) { // 排除表格

                variables.addVariable(parseVariable(expression));
            }
        }

        // 查找表格变量
        Matcher tableMatcher = TABLE_PATTERN.matcher(template);
        while (tableMatcher.find()) {
            String expression = tableMatcher.group(1);
            variables.addTableColumns(parseTableColumns(expression));
        }

        return variables;
    }

    public static TemplateVariables parseTemplate(String template, String prefix) {
        TemplateVariables variables = new TemplateVariables();

        // 查找普通变量
        Matcher varMatcher = VARIABLE_PATTERN.matcher(template);
        while (varMatcher.find()) {
            String expression = varMatcher.group(1);
            if (!expression.startsWith("tab:")) { // 排除表格
//                expression = removePrefixIfPresent(expression, prefix);
                variables.addVariable(parseVariable(expression,prefix));
            }
        }

        // 查找表格变量
        Matcher tableMatcher = TABLE_PATTERN.matcher(template);
        while (tableMatcher.find()) {
            String expression = tableMatcher.group(1);
            // 对于表格表达式，每个路径部分都需要单独处理
            List<TableColumn> columns = parseTableColumns(expression);
            for (TableColumn column : columns) {
                // 更新路径剔除前缀
                column.setPath(removePrefixIfPresent(column.getPath(), prefix));
            }
            variables.addTableColumns(columns);
        }

        return variables;
    }

    /**
     * 剔除路径中可能存在的指定前缀
     *
     * @param path 原始路径
     * @return 剔除前缀后的路径
     */
    private static String removePrefixIfPresent(String path, String prefix) {
        if (StringUtils.isNotBlank(prefix) && path.startsWith(prefix)) {
            // 只剔除第一个匹配的前缀
            return path.substring(prefix.length());
        }
        return path;
    }

    private static TemplateVariable parseVariable(String expression) {
        TemplateVariable variable = new TemplateVariable();
        variable.setFullExpression(expression);
        variable.setPath(expression);
        variable.setOriginalExpression(expression); // 存储原始表达式
        // 这里可以添加更多逻辑处理显示名等
        return variable;
    }

    private static TemplateVariable parseVariable(String expression,String prefix) {
        TemplateVariable variable = new TemplateVariable();
        variable.setOriginalExpression(expression); // 存储原始表达式
        String[] vs = expression.split("\\.");
        variable.setTypeName(vs[0]);
        // 存储不带前缀的路径用于取值
        variable.setPath(removePrefixIfPresent(expression,prefix));

        return variable;
    }


    private static List<TableColumn> parseTableColumns(String expression) {
        List<TableColumn> columns = new ArrayList<>();

        Matcher colMatcher = COLUMN_PATTERN.matcher(expression);
        while (colMatcher.find()) {
            String colExpression = colMatcher.group();
            TableColumn column = new TableColumn();

            String[] parts = colExpression.split("\\|");
            if (parts.length > 1) {
                column.setPath(parts[0].trim());
                column.setDisplayName(parts[1].trim());
            } else {
                column.setPath(colExpression.trim());
                // 使用最后路径段作为显示名
                String[] pathParts = colExpression.split("\\.");
                column.setDisplayName(pathParts[pathParts.length - 1]);
            }

            columns.add(column);
        }

        return columns;
    }

}
